import { createRouter, createWebHistory } from "vue-router";

import Home from "../components/Home/HomeView.vue";

const routes = [
  { path: "/home", component: Home },
  {
    path: "/welfare",
    component: () => import("../components/Welfare/WelfareView.vue"),
  },
  {
    path: "/CustomerService",
    component: () =>
      import("../components/CustomerService/CustomerServiceView.vue"),
  },
  { path: "/my", component: () => import("../components/My/MyView.vue") },
  {
    path: "/address",
    component: () => import("../components/My/AddressView.vue"),
  },
  { path: "/bill", component: () => import("../components/My/BillView.vue") },
  {
    path: "/delivery",
    component: () => import("../components/My/DeliveryView.vue"),
  },
  {
    path: "/discount",
    component: () => import("../components/My/DiscountView.vue"),
  },
  {
    path: "/friends",
    component: () => import("../components/My/FriendsView.vue"),
  },
  { path: "/help", component: () => import("../components/My/HelpView.vue") },
  { path: "/props", component: () => import("../components/My/PropsView.vue") },
  {
    path: "/receipt",
    component: () => import("../components/My/ReceiptView.vue"),
  },
  {
    path: "/services",
    component: () => import("../components/My/ServicesView.vue"),
  },
  { path: "/set", component: () => import("../components/My/SetView.vue") },
  { path: "/us", component: () => import("../components/My/UsView.vue") },

  { path: "/shippingsuccess", component: () => import("../components/My/ShippingSuccess.vue") },
  { path: "/logistics", component: () => import("../components/My/Logistics.vue") },
  { path: "/login", component: () => import("../components/Login/Loginview.vue") },
  { path: '/forgot', component: () => import("../components/Login/Forgot-password.vue") },
  { path: '/register', component: () => import("../components/Login/Register.vue") },

  {
    path: "/login",
    component: () => import("../components/Login/Loginview.vue"),
  },
  {
    path: "/forgot",
    component: () => import("../components/Login/Forgot-password.vue"),
  },
  {
    path: "/register",
    component: () => import("../components/Login/Register.vue"),
  },
  // 热销商品跳转到购买详情页
  {
    path: '/hotView/:id',
    name: 'hotBuyView',
    component: () => import('../components/Home/HotBuyView.vue')
  },

  { path: "/", redirect: "/home" },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});


router.beforeEach((to, from, next) => {
  if (to.path === "/login" || to.path === "/register" || to.path === "/forgot") {
    next();
  } else {
    // 检查用户是否已登录
    const token = sessionStorage.getItem("longtermToken");
    if (token) {
      next();
    } else {
      next("/login");
    }
  }
})


export default router;
